﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZD_Entities;
using ZD_Utility;
using System.Linq.Expressions;
using System.Reflection;


public class MyData : YouData
{
}

public class YouData
{
    public int v1 { get; set; }
    public int v2 { get; set; }
}

public class Service
{
    public int count(Expression<Func<YouData, bool>> exp)
    {
        //便于测试
        return 1;
    }
}

//重写Expression 访问类
public class MyExpressionVisitor : System.Linq.Expressions.ExpressionVisitor
{
    public ParameterExpression Parameter
    {
        get;
        set;
    }

    public Expression ModifyExp(Expression exp)
    {
        return this.Visit(exp);
    }

    protected override Expression VisitParameter(ParameterExpression node)
    {
        return Parameter;
    }
}

public class MyApp
{
    public int count(Expression<Func<MyData, bool>> exp)
    {
        ParameterExpression param = Expression.Parameter(typeof(YouData), "name");
        MyExpressionVisitor expVisitor = new MyExpressionVisitor();
        expVisitor.Parameter = param;
        Expression expBody = expVisitor.ModifyExp(exp.Body);
        var finalExp = Expression.Lambda<Func<YouData, bool>>(expBody, param);
        return new Service().count(finalExp);
    }
}

public partial class Default2 : BasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        MyApp a = new MyApp();
        Response.Write(a.count(data => data.v1 == 0));
        //a.count(data => data.v1 == 0);
    }


    protected void test_Click(object sender, EventArgs e)
    {

    }
}